package util;

import java.util.ArrayList;
import java.util.ListIterator;

import model.HolidayCalendarDTO;
import model.LeaveDTO;
import model.LeaveTypeDTO;
import model.PositionDTO;
import model.UserDTO;
import bizlogic.HolidayCalendarManager;
import bizlogic.LeaveManager;
import bizlogic.LeaveTypeManager;
import bizlogic.PositionManager;
import bizlogic.UserManager;
import exception.DAOSystemException;

public class IdGenerator {
/**
 * @pranjal
 * @param idList
 * @return
 */
	public static Integer findLargestInt(ArrayList<Integer> idList)
	{
		Integer largestID = 0;
		ListIterator it = idList.listIterator();
		while(it.hasNext())
		{
			Integer i = (Integer)it.next();
			largestID = (largestID >= i)?largestID:i;
		}
		return largestID;
	}
	
	public static Integer generatePositionID()
	{
		PositionManager posman = new PositionManager();
		ArrayList<PositionDTO> posList = new ArrayList<PositionDTO>();

		try {
			posList = posman.findAllPosition();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Integer> idList = new ArrayList<Integer>();
		ListIterator it = posList.listIterator();
		while(it.hasNext())
		{
			PositionDTO p =(PositionDTO)it.next();
			Integer i = Integer.parseInt(p.getId().toString());
			idList.add(i);
		}
		
		return findLargestInt(idList)+1;
	}
	
	public static Integer generateHolidayCalendarID()
	{
		HolidayCalendarManager posman = new HolidayCalendarManager();
		ArrayList<HolidayCalendarDTO> posList = new ArrayList<HolidayCalendarDTO>();

		try {
			posList = posman.findAllHolidayCalendar();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Integer> idList = new ArrayList<Integer>();
		ListIterator it = posList.listIterator();
		while(it.hasNext())
		{
			HolidayCalendarDTO p =(HolidayCalendarDTO)it.next();
			Integer i = Integer.parseInt(p.getId().toString());
			idList.add(i);
		}
		
		return findLargestInt(idList)+1;
	}
	
	public static Integer generateLeaveTypeID()
	{
		LeaveTypeManager posman = new LeaveTypeManager();
		ArrayList<LeaveTypeDTO> posList = new ArrayList<LeaveTypeDTO>();

		try {
			posList = posman.findAllLeaveType();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Integer> idList = new ArrayList<Integer>();
		ListIterator it = posList.listIterator();
		while(it.hasNext())
		{
			LeaveTypeDTO p =(LeaveTypeDTO)it.next();
			Integer i = Integer.parseInt(p.getId().toString());
			idList.add(i);
		}
		
		return findLargestInt(idList)+1;
	}

	public static Integer generateUserID()
	{
		UserManager posman = new UserManager();
		ArrayList<UserDTO> posList = new ArrayList<UserDTO>();

		try {
			posList = posman.findAllUser();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Integer> idList = new ArrayList<Integer>();
		ListIterator it = posList.listIterator();
		while(it.hasNext())
		{
			UserDTO p =(UserDTO)it.next();
			Integer i = Integer.parseInt(p.getId().toString());
			idList.add(i);
		}
		
		return findLargestInt(idList)+1;
	}
	
	public static Integer generateLeavID()
	{
		LeaveManager posman = new LeaveManager();
		ArrayList<LeaveDTO> posList = new ArrayList<LeaveDTO>();

		try {
			posList = posman.findAllLeave();
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Integer> idList = new ArrayList<Integer>();
		ListIterator it = posList.listIterator();
		while(it.hasNext())
		{
			LeaveDTO p =(LeaveDTO)it.next();
			Integer i = Integer.parseInt(p.getId().toString());
			idList.add(i);
		}
		
		return findLargestInt(idList)+1;
	}
}
